﻿using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Tom.Net;

namespace Omabar.Entity
{
    public class Hotel_Gallery_Entity : EntityBase<HOTEL_GALLERY>
    {
        public override RESP_Info Add(HOTEL_GALLERY model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "insert into HOTEL_GALLERY(HOTEL_ID,PHOTO_ID) values (@HOTEL_ID,@PHOTO_ID)";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("HOTEL_ID", model.HOTEL_ID));
                    cmd.Parameters.Add(new SqlParameter("PHOTO_ID", model.PHOTO_ID));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "成功";
                    info.Tip = "<h5><p>添加酒店图片成功</p></h5>";
                    info.Url = "HotelList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>添加酒店图片失败</p></h5>";
                    info.Url = "HotelList.aspx";

                    Tom.Net.Log.LogManager.Error("添加酒店图片失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Update(HOTEL_GALLERY model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "update HOTEL_GALLERY set PHOTO_ID=@PHOTO_ID where HOTEL_ID=@HOTEL_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("HOTEL_ID", model.HOTEL_ID));
                    cmd.Parameters.Add(new SqlParameter("PHOTO_ID", model.PHOTO_ID));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "编辑";
                    info.Tip = "<h5><p>编辑酒店图片成功</p></h5>";
                    info.Url = "HotelList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>编辑酒店图片失败</p></h5>";
                    info.Url = "HotelAdd.aspx";

                    Tom.Net.Log.LogManager.Error("编辑酒店图片失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Delete(HOTEL_GALLERY hotel_Gallery)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delete from HOTEL_GALLERY where PHOTO_ID=@PHOTO_ID and HOTEL_ID=@HOTEL_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("HOTEL_ID", hotel_Gallery.HOTEL_ID));
                    cmd.Parameters.Add(new SqlParameter("PHOTO_ID", hotel_Gallery.PHOTO_ID));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "删除";
                    info.Tip = "<h5><p>删除酒店图片成功</p></h5>";
                    info.Url = "HotelList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>删除酒店图片失败</p></h5>";
                    info.Url = "HotelList.aspx";

                    Tom.Net.Log.LogManager.Error("删除酒店图片失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override HOTEL_GALLERY GetModel(string id)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "select HOTEL_ID,PHOTO_ID from HOTEL_GALLERY where HOTEL_ID=@HOTEL_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("HOTEL_ID", id));

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        HOTEL_GALLERY hotelGallery = new HOTEL_GALLERY();
                        if (!reader.IsDBNull(0)) hotelGallery.HOTEL_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) hotelGallery.PHOTO_ID = reader.GetString(1);
                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return hotelGallery;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取酒店图片数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override HOTEL_GALLERY GetModelBySQL(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        HOTEL_GALLERY hotelGallery = new HOTEL_GALLERY();
                        if (!reader.IsDBNull(0)) hotelGallery.HOTEL_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) hotelGallery.PHOTO_ID = reader.GetString(1);

                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return hotelGallery;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取酒店图片数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override List<HOTEL_GALLERY> GetModels(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    List<HOTEL_GALLERY> hotelGallerys = new List<HOTEL_GALLERY>();
                    while (reader.Read())
                    {
                        HOTEL_GALLERY food = new HOTEL_GALLERY();
                        if (!reader.IsDBNull(0)) food.HOTEL_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) food.PHOTO_ID = reader.GetString(1);

                        hotelGallerys.Add(food);
                    }
                    reader.Close();
                    reader.Dispose();
                    cmd.Dispose();

                    return hotelGallerys;
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取酒店图片数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return null;
        }
    }
}
